import 'package:baselib/utils/color_base_util.dart';
import 'package:baselib/utils/px_base_util.dart';
import 'package:flutter/material.dart';

class CommMessageDialog extends Dialog {
  String message;

  String negativeText;
  String positiveText;
  Function onCloseEvent;
  Function onPositivePressEvent;
  bool isTouchOutSideDissmiss;
  bool isTouchKeyBackDissmiss;


  CommMessageDialog({
    Key key,
    @required this.message,
    this.negativeText,
    this.positiveText,
    this.onPositivePressEvent,
    this.isTouchOutSideDissmiss=true,
    this.isTouchKeyBackDissmiss=true,
    @required this.onCloseEvent,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: (){
        if(isTouchKeyBackDissmiss){
          Navigator.of(context).pop();
        }
      },
      child: GestureDetector(
        onTap: (){
          if(isTouchOutSideDissmiss){
            Navigator.of(context).pop();
          }
        },
        child: new Material(
          type: MaterialType.transparency,
          child: new Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              GestureDetector(
                  onTap: (){

                  },
                  child:new Container(
                    decoration: ShapeDecoration(
                      color: ColorBaseUtil.instance.color_white,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(
                          Radius.circular(PxBaseUtil.instance.px_10),
                        ),
                      ),
                    ),
                    constraints: BoxConstraints(
                      // minHeight: ScreenUtil.getInstance().getAdapterSize(PxUtils.px_65),
                        maxWidth: PxBaseUtil.instance.screenWidth*3/4
                    ),
                    margin:  EdgeInsets.only(
                        bottom: PxBaseUtil.instance.statusBarHeight
                    ),
                    child: new Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        new Container(
                          alignment: Alignment.center,
                          constraints: BoxConstraints(minHeight: PxBaseUtil.instance.px_65),
                          padding: EdgeInsets.fromLTRB(PxBaseUtil.instance.px_15, PxBaseUtil.instance.px_10,
                              PxBaseUtil.instance.px_15, PxBaseUtil.instance.px_15),
                          child: new Text(
                            message,
                            style: TextStyle(fontSize: PxBaseUtil.instance.px_14,color: ColorBaseUtil.instance.color_455262),
                          ),
                        ),

                        new Container(
                          color:  ColorBaseUtil.instance.color_e0e0e0,
                          height: PxBaseUtil.instance.px_0d5,
                        ),
                        Container(
                          child: this._buildBottomButtonGroup(context),
                        )
                      ],
                    ),
                  ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildBottomButtonGroup(BuildContext context) {
    var widgets = <Widget>[];
    if (negativeText != null && negativeText.isNotEmpty) widgets.add(_buildBottomCancelButton(context));
    widgets.add(new Container(
      color: ColorBaseUtil.instance.color_e0e0e0,
        width: PxBaseUtil.instance.px_0d5,
        height: PxBaseUtil.instance.px_20,
    ));
    if (positiveText != null && positiveText.isNotEmpty) widgets.add(_buildBottomPositiveButton(context));
    return new Flex(
      direction: Axis.horizontal,
      children: widgets,
    );
  }

  Widget _buildBottomCancelButton(BuildContext context) {
    return new Flexible(
      fit: FlexFit.tight,
      child: GestureDetector(
        onTap: onCloseEvent==null?(){
          Navigator.of(context).pop();
        }:onCloseEvent,
        child: Container(
          color: Colors.transparent,
          width: double.infinity,
          padding: EdgeInsets.all(PxBaseUtil.instance.px_10),
          alignment: Alignment.center,
          child: new Text(
            negativeText,
            textAlign: TextAlign.center,
            style: TextStyle(
              fontSize: PxBaseUtil.instance.px_14,
              color: ColorBaseUtil.instance.color_888888
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildBottomPositiveButton(BuildContext context) {
    return new Flexible(
      fit: FlexFit.tight,
      child: GestureDetector(
        onTap: (){
          Navigator.of(context).pop();
          onPositivePressEvent();
        },
        child: Container(
          color: Colors.transparent,
          width: double.infinity,
          padding: EdgeInsets.all(PxBaseUtil.instance.px_10),
          alignment: Alignment.center,
          child: new Text(
            positiveText,
            style: TextStyle(
              color: ColorBaseUtil.instance.color_blue,
              fontSize: PxBaseUtil.instance.px_14,
            ),
          ),
        ),
      ),
    );
  }
}